package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.comon.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //查询菜系和套餐
    @GetMapping("/category/findAll")
    public ResultInfo<List<Category>> findAll() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<Category>()
                .orderByAsc(Category::getSort);
        List<Category> categoryList = categoryService.list(wrapper);
        return ResultInfo.success(categoryList);
    }

    //添加菜系和套餐
    @PostMapping("/category")
    @CacheEvict(value = "category", allEntries = true)
    public ResultInfo<Object> addStyleOfCooking(@RequestBody Category category) {
        categoryService.save(category);
        return ResultInfo.success(null);
    }

    //更新菜系和套餐
    @PutMapping("/category")
    @CacheEvict(value = "category", allEntries = true)
    public ResultInfo<Object> updateStyleOfCooking(@RequestBody Category category) {
        categoryService.updateById(category);
        return ResultInfo.success(null);
    }

    //删除菜系和套餐
    @DeleteMapping("/category")
    @CacheEvict(value = "category", allEntries = true)
    public ResultInfo<Object> deleteStyleOfCooking(Long id) {
        categoryService.deleteStyleOfCooking(id);
        return ResultInfo.success(null);
    }

    //查询菜系
    @GetMapping("/category/list")
    public ResultInfo<List<Category>> findType(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType, type);
        return ResultInfo.success(categoryService.list(wrapper));
    }
}
